package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.util.DayWeekMoonTImeUtil;
import com.aduerr.v1shouyin.util.DoubleClickUtils;
import com.aduerr.v1shouyin.view.pickerview.TimePicker2View;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 对账页面的时间选择页面
 */
public class HomeBillTimeSelectActivity extends AppCompatActivity {
    @BindView(R.id.img_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.rl_today)
    RelativeLayout mRlToday;
    @BindView(R.id.rl_yesterday)
    RelativeLayout mRlYesterday;
    @BindView(R.id.rl_near7day)
    RelativeLayout mRlNear7;
    @BindView(R.id.rl_current_month)
    RelativeLayout mRlCurMonth;
    @BindView(R.id.rl_last_month)
    RelativeLayout mRlLastMonth;
    @BindView(R.id.rl_custom)
    RelativeLayout mRlCustom;
    @BindView(R.id.ll_show_custom_time)
    LinearLayout mLlShowCustomTime;

    @BindView(R.id.tv_today)
    TextView mTvToday;
    @BindView(R.id.tv_yesterday)
    TextView mTvYesterday;
    @BindView(R.id.tv_near7day)
    TextView mTvNear7;
    @BindView(R.id.tv_current_month)
    TextView mTvCurMonth;
    @BindView(R.id.tv_last_month)
    TextView mTvLastMonth;
    @BindView(R.id.tv_custom)
    TextView mTvCustom;

    @BindView(R.id.tv_begin_date)
    TextView mTvBeginDate;
    @BindView(R.id.tv_begin_time)
    TextView mTvBeginTime;
    @BindView(R.id.tv_end_date)
    TextView mTvEndDate;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R.id.btn_ok)
    Button mBtOk;

    public String beginTime = "";
    public String endTime = "";
    public String timeName = "今天";
    private TimePicker2View pvTime2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_bill_time_select);
        ButterKnife.bind(this);
        beginTime = DayWeekMoonTImeUtil.getTodayBeginTime();
        endTime = DayWeekMoonTImeUtil.getTodayEndTime();
        initTimePicker();
    }

    @OnClick({R.id.img_back, R.id.rl_today, R.id.rl_yesterday,R.id.rl_near7day, R.id.rl_current_month,
            R.id.rl_last_month, R.id.rl_custom, R.id.ll_show_custom_time,R.id.btn_ok})
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.img_back:
                finish();
                break;

            case R.id.rl_today:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.rl_today)) {
                    beginTime = DayWeekMoonTImeUtil.getTodayBeginTime();
                    endTime = DayWeekMoonTImeUtil.getTodayEndTime();
                    timeName = "今天";
                    clearAllChecked();
                    mTvToday.setSelected(true);
                }
                break;

            case R.id.rl_yesterday:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.rl_yesterday)) {
                    beginTime = DayWeekMoonTImeUtil.getYestdayBeginTime();
                    endTime = DayWeekMoonTImeUtil.getYesdayEndTime();
                    timeName = "昨天";
                    clearAllChecked();
                    mTvYesterday.setSelected(true);
                }
                break;


            case R.id.rl_near7day:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.rl_near7day)) {
                    beginTime = DayWeekMoonTImeUtil.getLastWeekBeginTime();
                    endTime = DayWeekMoonTImeUtil.getLastWeekEndTime();
                    timeName = "近7天";
                    clearAllChecked();
                    mTvNear7.setSelected(true);
                }
                break;

            case R.id.rl_current_month:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.rl_current_month)) {
                    beginTime = DayWeekMoonTImeUtil.getThisMoonBeginTime();
                    endTime = DayWeekMoonTImeUtil.getThisMoonEndTime();
                    timeName = "本月";
                    clearAllChecked();
                    mTvCurMonth.setSelected(true);
                }
                break;

            case R.id.rl_last_month:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.rl_last_month)) {
                    beginTime = DayWeekMoonTImeUtil.getLastMoonBeginTime();
                    endTime = DayWeekMoonTImeUtil.getLastMoonEndTime();
                    timeName = "上月";
                    clearAllChecked();
                    mTvLastMonth.setSelected(true);
                }
                break;

            case R.id.rl_custom:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.rl_custom)) {
                    clearAllChecked();
                    mTvCustom.setSelected(true);
                    mLlShowCustomTime.setVisibility(View.VISIBLE);
                    timeName = "自定义";
                    beginTime = DayWeekMoonTImeUtil.getTodayBeginTime();
                    endTime = DayWeekMoonTImeUtil.getTodayEndTime();
                    mTvBeginDate.setText(beginTime.split(" ")[0]);
                    mTvBeginTime.setText(beginTime.split(" ")[1].substring(0, 5));
                    mTvEndDate.setText(endTime.split(" ")[0]);
                    mTvEndTime.setText(endTime.split(" ")[1].substring(0, 5));
                }
                break;
            case R.id.ll_show_custom_time:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.ll_show_custom_time)) {
                    pvTime2.show();
                }
                break;
            case R.id.btn_ok:
                if(!DoubleClickUtils.isFastDoubleClick(R.id.btn_ok)) {
                    Intent intent = getIntent();
                    intent.putExtra("time_id", timeName);
                    intent.putExtra("begin_time", beginTime);
                    intent.putExtra("end_time", endTime);
                    setResult(68,intent);
                    finish();
                }
                break;


        }
    }


    private void clearAllChecked() {
        mTvToday.setSelected(false);
        mTvYesterday.setSelected(false);
        mTvNear7.setSelected(false);
        mTvCurMonth.setSelected(false);
        mTvLastMonth.setSelected(false);
        mTvCustom.setSelected(false);
        mLlShowCustomTime.setVisibility(View.GONE);
    }


    private void initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(2013, 1, 23);
        final Calendar endCalendar = Calendar.getInstance();

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);//获取年份
        int month = cal.get(Calendar.MONTH);//获取月份
        int day = cal.get(Calendar.DATE);//获取日
        int hour = cal.get(Calendar.HOUR_OF_DAY);//小时
        int minute = cal.get(Calendar.MINUTE);//分
        endCalendar.set(year, month, day, hour, minute);

        pvTime2 = new TimePicker2View.Builder(this,
                new TimePicker2View.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v, Date date2) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                Date curDate = new Date(System.currentTimeMillis());
                if(!date2.before(date)) {
                    if(date2.before(curDate) && date.before(curDate)) {
                        //时间选择器
                        mTvCustom.setSelected(true);
                        //==================
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

                        String formatstart = sdf.format(date);
                        String[] splitstart = formatstart.split(" ");
                        mTvBeginDate.setText(splitstart[0]);
                        mTvBeginTime.setText(splitstart[1]);

                        String formatend = sdf.format(date2);
                        String[] splitend = formatend.split(" ");
                        mTvEndDate.setText(splitend[0]);
                        mTvEndTime.setText(splitend[1]);

                        StringBuffer stringBuffer = new StringBuffer();
                        beginTime = sdf.format(date) + ":00";
                        endTime = sdf.format(date2) + ":59";
                        stringBuffer.append("从" + beginTime);
                        stringBuffer.append("至" + endTime);


                        Calendar c = Calendar.getInstance();
                        c.setTime(date);
                        c.set(Calendar.HOUR_OF_DAY, 0);                  //把当前时间小时变成０
                        c.set(Calendar.MINUTE, 0);                      //把当前时间分钟变成０
                        c.set(Calendar.SECOND, 0);                      //把当前时间秒数变成０
                        c.set(Calendar.MILLISECOND, 0);                 //把当前时间毫秒变成０


                        c.setTime(date2);
                        c.set(Calendar.HOUR_OF_DAY, 23);
                        //把当前时间小时变成23
                        c.set(Calendar.MINUTE, 59);                      //把当前时间分钟变成59
                        c.set(Calendar.SECOND, 59);                      //把当前时间秒数变成59
                        c.set(Calendar.MILLISECOND, 999);


                    } else {
                        Toast.makeText(HomeBillTimeSelectActivity.this, "时间不能大于当前时间",
                                Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(HomeBillTimeSelectActivity.this,
                            getResources().getString(R.string.pickerview_choosedate_error), Toast.LENGTH_SHORT).show();
                }
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
//                .setType(new boolean[]{true, true, true, false, false, false})
                .setType(new boolean[]{true, true, true, true, true, false})
                .setLabel("年", "月", "日", "时", "分", "")
//                .setLabel("年", "月", "日", "", "", "")
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(16)
                .setDate(selectedDate, selectedDate)
                .setRangDate(startCalendar, endCalendar, startCalendar, endCalendar)
                .setBackgroundId(0x33000000) //设置外部遮罩颜色
                .setDecorView(null)
                .setTitleText(getResources().getString(R.string.pickerview_choosedate))
                .setTitleBgColor(getResources().getColor(R.color.home_main_color))
                .setTitleColor(getResources().getColor(R.color.white))
                .setSubmitColor(getResources().getColor(R.color.white))
                .setCancelColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(android.R.color.transparent))
                .build();
    }


}
